﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 档案类型与档案模板
    /// </summary>
    public class T_ArchiveFieldsDal
    {
        static SGArchiveSystem7xEntities conn;
       /// <summary>
       /// 建立档案字段(初始化模板建立)
       /// </summary>
       /// <param name="aid">ArchiveTree‘s id</param>
       /// <param name="userName">操作的用户姓名</param>
       /// <returns></returns>
         public static _ReturnValue Add_ArchiveFieldsInit(int aid, string userName)
        {
            
            conn = new SGArchiveSystem7xEntities();
            return Add_ArchiveFieldsInit(conn, aid, userName);
        }
        /// <summary>
        /// 建立档案字段(初始化模板建立，不初始化数据库连接，由其他方法传入)
        /// </summary>
        /// <param name="aid">ArchiveTree‘s id</param>
        /// <param name="userName">操作的用户姓名</param>
        /// <returns></returns>
        public static _ReturnValue Add_ArchiveFieldsInit(SGArchiveSystem7xEntities conn, int aid, string userName)
        {
            try {
                DateTime time = DateTime.Now;
                //首先判断此模板是否存在
                if (conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_PID == aid).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var list = T_ArchiveFieldsModelDal.Get_ArchiveFieldsModel(conn);
                    var listNew = new List<T_ArchiveFields>();
                    foreach (var n in list)
                    {
                        T_ArchiveFields en = new T_ArchiveFields();
                        if (!SGArchiveSystem7xCommons.SGEntity.ModifyEntity(en, n, "AF_ID"))
                            return _ReturnValue.Err;
                        en.AF_PID = aid;
                        en.AF_LASTDATETIME = time;
                        en.AF_LASTUSERNAME = userName;
                        listNew.Add(en);
                    }
                    conn.T_ArchiveFields.AddRange(listNew);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// 添加某一个字段
        /// </summary>
        /// <returns></returns>
        public static _ReturnValue Add_ArchiveFields(T_ArchiveFields en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_PID == en.AF_PID && c.AF_NAME == en.AF_NAME).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    #region 获取当前最大的T开的字段，来计算应该使用哪个
                    var max = conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_FIELD.StartsWith("T") && !c.AF_FIELD.Equals("TM")).OrderByDescending(c => c.AF_FIELD).ToList();
                    if (max.Count() > 0)
                    {
                        var temp = max[0].AF_FIELD;
                        temp = temp.Substring(1);
                        en.AF_FIELD = "T" + (int.Parse(temp) + 1).ToString().PadLeft(2, '0');
                    }
                    else
                        en.AF_FIELD = "T01";
                    #endregion

                    conn.T_ArchiveFields.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch
            {
                return _ReturnValue.Err;
            }
        }

        /// <summary>
        /// 修改某个档案的字段
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_ArchiveFileds(T_ArchiveFields en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_ID != en.AF_ID && c.AF_PID == en.AF_PID && c.AF_NAME == en.AF_NAME).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_ID == en.AF_ID);
                    foreach(var n in query)
                    {
                        ///字段修改时，只允许修改部分内容
                        n.AF_NAME = en.AF_NAME;
                        n.AF_ORDER = en.AF_ORDER;
                        n.AF_QUERY = en.AF_QUERY;
                        n.AF_SHOW = en.AF_SHOW;
                        n.AF_STAT = en.AF_STAT;
                        n.AF_LEN = en.AF_LEN;
                        n.AF_ISNULL = en.AF_ISNULL;
                        n.AF_LASTDATETIME = en.AF_LASTDATETIME;
                        n.AF_LASTUSERNAME = en.AF_LASTUSERNAME;
                        n.AF_EDIT = en.AF_EDIT;
                        //if (!SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "AF_FIELD"))
                           /// return _ReturnValue.Err;
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch(Exception ex) { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 删除或恢复某个字段
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status">0, 删除， 1. 正常</param>
        /// <returns></returns>
        public static _ReturnValue Modify_ArchiveFields(int id, int status)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_ID == id);
                foreach (var n in query)
                    n.AF_STAT = status;
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }


        /// <summary>
        /// 只修改名字
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_ArchiveFields(int id, string name)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_ID != id && c.AF_NAME == name).Count() > 0)
                    return _ReturnValue.Already;
                var query = conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_ID == id);
                foreach (var n in query)
                    n.AF_NAME = name;
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>

        /// <summary>
        /// 查询某个档案类型的字段
        /// </summary>
        /// <returns></returns>
        /// <param name="count">总数</param>
        /// <param name="pid">档案类型ID</param>
        /// <param name="status">状态</param>
        public static List<T_ArchiveFields> Get_ArchiveFields(out int count, int status = 1, int pid = -1, int start = -1, int row = -1, int show = 1, int que = 1, int edit = 1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveFields.Select(c => c);
                if (status > -1)
                    query = query.Where(c => c.AF_STATUS == status);
                if (pid > -1)
                    query = query.Where(c => c.AF_PID == pid);
                if (show > -1)
                    query = query.Where(c => c.AF_SHOW == show);
                if (que > -1)
                    query = query.Where(c => c.AF_QUERY == que);
                if (edit > -1)
                    query = query.Where(c => c.AF_EDIT == edit);
                if(start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.AF_PID).ThenBy(c => c.AF_ORDER).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list =  query.OrderBy(c => c.AF_PID).ThenBy(c => c.AF_ORDER).ToList();
                    count = list.Count();
                    return list;
                }
            }
            catch { return null; }
        }

        /// <summary>
        /// 根据ID获取一个档案字段
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T_ArchiveFields Get_ArchiveFields(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_ID == id).Single();
            }
            catch { return null; }
        }

        /// <summary>
        /// 获取某个档案类型的所有字段
        /// </summary>
        /// <param name="pd"></param>
        /// <returns></returns>
        public static List<T_ArchiveFields> Get_ArchiveFieldsForArchive(int pid)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_ArchiveFields.Select(c => c).Where(c => c.AF_PID == pid).OrderBy(c => c.AF_ORDER).ToList();
            }
            catch { return null; }
        }
    }
}
